package com.wt.admin.controller;

import com.wt.admin.entity.LoginAccount;
import com.wt.admin.entity.Register;
import com.wt.admin.entity.SmsCode;
import com.wt.admin.service.WqunAdminService;
import com.wt.commonutils.R;
import com.wt.commonutils.ResultMess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 账号表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-07-14
 */
@Api(tags = "登录注册模块")
@RestController
@RequestMapping("/loginservice/account")
@CrossOrigin
public  class AccountController {
    @Autowired
    private WqunAdminService accountService;

    @Autowired
    RedisTemplate redisTemplate;

    ResultMess resultMess = new ResultMess();

    //注册用户
    @ApiOperation("注册账号")
    @PostMapping("registerAccount")
    public R registerAccount(@RequestBody Register register){
        //判断用户是否注册成功
        if(accountService.registerAccount(register)){
            return R.ok().message(resultMess.REGISTER_SUCCESS_PHONE);
        }else{
            return R.info().message(resultMess.ERROR_UNDEFINED);
        }
    }

    //用户登录
    @ApiOperation("用户登录-验证码登录")
    @PostMapping("loginCode")
    public R loginCode(@RequestBody SmsCode smsCode){
        //登录验证
        String token =  accountService.loginCode(smsCode);
        //将登录凭证发给前端
        return R.ok().message(resultMess.LOGIN_SUCCESS_PHONE_CODE_ALIKE).data("token",token);
    }

    //用户登录-账号密码登录
    @ApiOperation("用户登录-账号密码登录")
    @PostMapping("loginAccount")
    public R loginAccount(@RequestBody LoginAccount loginAccount){
        //登录验证
        String token = accountService.loginAccount(loginAccount);
        //将登录凭证发给前端
        return R.ok().message(resultMess.LOGIN_SUCCESS_PHONE_CODE_ALIKE).data("token",token);
    }

}

